/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package appClient;

import com.google.gson.Gson;
import engine.Address;
import engine.City;
import engine.Country;
import engine.FullPersone;
import engine.GType;
import engine.Persone;
import engine.Phone;
import engine.Singleton;
import engine.Street;
import engine.TypeAdress;
import engine.TypePhone;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import protocol.Command;
import server.Player;
import server.ServeOneThread;
import sql.Conector;
import webClient.HttpClient;
import webClient.HttpClient.SocketProcessor;


/**
 *
 * @author User
 */
public class UserComman {

             
    private List<Persone> persone = new ArrayList<Persone>();

  public static void connection(String login, String pass, VisualClien vis) throws IOException {
    ConnectClient cli = new ConnectClient(login, pass, "app");
    vis.setClient(cli);
  }
  
  public static void connection(String login, String pass, SocketProcessor socProc) throws IOException {
    ConnectClient cli = new ConnectClient(login, pass, "web");
    Singleton.cli = cli;
  }
  
  private Player getPlayer(String name)
  {
      Player player = null;
      for (Player p : Singleton.clients)
      {
          if (p.getLogin().equals(name)){
              player = p;
          }
      }
      return player;
  }
  
//  public static void setRole(ConnectClient connectClient, String role, String add) {
//    if (role.equals("Error")){
//      System.out.println("not login");
//    }
//    if (role.equals("Admin")){
//      connectClient.role = "Admin";
//      connectClient.vis.makeVisible();
//      connectClient.Uid = add;
//      getList(connectClient);
//    }
//    if (role.equals("SUser")){
//      connectClient.role = "SUser";
//      connectClient.vis.makeVisible();
//      connectClient.Uid = add;
//      getList(connectClient);
//    }
//    if (role.equals("User")){
//      connectClient.role = "User";
//      connectClient.vis.makeInvisible();
//      connectClient.Uid = add;
//      getList(connectClient);
//    }
//    
//    
//  }
  
//  public static boolean autorize(String name, String pass) throws SQLException
//  {
//    Statement statement = Conector.conect();
//    try {
//      String query = "SELECT pass, id, rollid FROM user WHERE LOGIN='" + name + "'";
//      ResultSet resultSet = statement.executeQuery(query);
//      resultSet.next();
//      String passSql = resultSet.getString(1);
//      String usId = resultSet.getString(2);
//      String rollId = resultSet.getString(3);
//      
//      query = "SELECT nroll FROM roll WHERE id='" + rollId + "'";
//      resultSet = statement.executeQuery(query);
//      resultSet.next();
//      String roll = resultSet.getString(1);
//      
//      Player palyer = Singleton.clients.get(Singleton.clients.size()-1);
//      Command obj = new Command();
//      obj.Type = "Connect";
//      if (!passSql.equals(pass))
//      {
//          obj.Data = "Error";
//          Gson gson = new Gson();
//          String send = gson.toJson(obj);
//          palyer.getSrv().sendUser(send);
//         // vis.getCli().refuse("{\"Type\": \"End\"}");
//      }
//      else{
//          
//          obj.Data = roll;
//          Gson gson = new Gson();
//          String send = gson.toJson(obj);
//          palyer.getSrv().sendUser(send);
//          
//          palyer.setLogin(name);
//          palyer.setId(usId);
//          palyer.setRole(roll);
//      }
//      } catch (Exception e)
//        {
//          //  log.info("test" + e);
//          Player palyer = Singleton.clients.get(Singleton.clients.size()-1);
//          Command obj = new Command();
//          obj.Type = "Connect";
//          obj.Data = "Error";
//          Gson gson = new Gson();
//          String send = gson.toJson(obj);
//          palyer.getSrv().sendUser(send);
//        }
//      
//      String query = "SHUTDOWN";
//      statement.execute(query);
//      return true;
//  }
  
//  public static boolean delPersone(FullPersone fp, ServeOneThread mysrv) throws SQLException {
//    Statement statement = Conector.conect();
//    String query = "DELETE FROM persone WHERE id = '" + fp.ID + "'";
//    statement.executeUpdate(query);
//    
//    for (Phone p : fp.Phones)
//    {
//      query = "DELETE FROM phone WHERE id = '" + p.ID + "'";
//      statement.executeUpdate(query);
//    }
//    
//    for (Address p : fp.Addres)
//    {
//      query = "DELETE FROM adres WHERE id = '" + p.ID + "'";
//      statement.executeUpdate(query);
//    }
//
//    query = "SHUTDOWN";
//    statement.execute(query);
//    return true;
//  }
  
  public static String commandGet(String cmd, String add)
  {
    Gson gson = new Gson();
    Command obj = new Command();
    obj.Type = "Get";
    GType gt = new GType();
    gt.Type = cmd;
    obj.Data = gson.toJson(gt);
    obj.Add = add;
    String json = gson.toJson(obj);
    return json;
  }

  
  public static boolean getList(ConnectClient cli)
  {
    cli.refuse(commandGet("List", null));
     return true;
  }
  
  public static boolean getInfo(ConnectClient cli, String id)
  {
      cli.refuse(commandGet("Person", id));
     return true;
  }
     

  
  
      
}
